;
! function() {
	var layer = layui.layer,
		form = layui.form,
		laypage = layui.laypage;

	to_page(1, 10);

	// 搜索点击事件
	$("#searchBtn").click(function() {
		to_page(1, 10);
	});
	
	// 选择状态后查询
	form.on('select(status)', function(data) {
		to_page(1, 10);
	});

	// 回车键搜索
	$("body").keydown(function() {
		if(event.keyCode == "13") {
			to_page(1, 10);
		}
	});
	
	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/refund/",
			type: "post",
			data: {
				"pageNum": pageNum,
				"pageSize": pageSize,
				"status": $("#status").val(),
				"phone": $("#searchBox").val()
			},
			async: false,
			success: function(result) {
				if(result.totalRow == 0) {
					// 提示没有数据
					$("#content").html("<tr><td colspan='8' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装tr并赋给页面
					build_table(result);
					if(result.totalRow > 10) {
						// 拼装分页总览信息并赋给页面
						build_pageInfo(result);
						// 显示分页按钮
						laypage.render({
							elem: 'page_nav_div',
							count: result.totalRow,
							curr: result.pageNumber,
							limit: result.pageSize,
							theme: "#FF6700",
							prev: "<i class='layui-icon'>&#xe65a;</i>",
							next: "<i class='layui-icon'>&#xe65b;</i>",
							layout: ['prev', 'page', 'next', 'limit', 'skip'],
							jump: function(obj, first) {
								if(!first) {
									to_page(obj.curr, obj.limit);
								}
							}
						});
						// 显示分页条
						$("#page_info").show();
					}
					// 退款人手机号搜索提示
					$.typeahead({
						input: ".js-typeahead",
						order: "asc",
						source: {
							groupName: {
								ajax: {
									url: "/refund/getAllPhone"
								}
							}
						}
					});
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + pageInfo.pageNumber + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var orderNoTd = $("<td></td>").append(this.orderNo);
			var phoneTd = $("<td></td>").append(this.phone);
			var amountTd = $("<td></td>").append(this.amount + "元");
			var statusTd = $("<td></td>").append(this.status);
			var payPlatformTd = $("<td></td>").append(this.payPlatform);
			var applicationTimeTd = $("<td></td>").append(this.applicationTime);
			var refundTimeTd = $("<td></td>").append(this.refundTime);
			var operationTd = $("<td></td>").append(this.operationHtml);
			// 赋给tbody
			$("<tr id='" + this.id + "' job-id='" + this.jobId + "'></tr>").append(orderNoTd).append(phoneTd).append(amountTd)
				.append(statusTd).append(payPlatformTd).append(applicationTimeTd).append(refundTimeTd).append(operationTd).appendTo("#content");
		});
	}

	// 查看任务详情
	$(document).on("click", ".check-job-btn", function() {
		var jobId = $(this).parent().parent().attr("job-id");
		if(jobId != null && jobId.length == 32) {
			// 查询任务信息
			$.ajax({
				url: "/bizjobs/getJobById",
				type: "post",
				data : {
					"jobId": jobId
				},
				success: function(result) {
					var job = result.data;
					if(result.status == 0) {
						// 赋值给td
						$(".job-title").text(job.title);
						$(".job-whetherPay").text(job.whetherPay);
						$(".job-auditStatus").text(job.auditStatus);
						$(".job-status").text(job.status);
						$(".job-price").text(job.price + "元");
						$(".job-createTime").text(job.createTime);
						$(".job-deadTime").text(job.deadTime);
						
						// 显示弹窗
						layer.open({
							type: 1,
							move: false,
							title: false,
							content: $("#job-details-div"),
							cancel: function() {
								$("#job-details-div").hide();
							}
						});
					} else {
						layer.msg(result.msg, {
							icon : 5
						});
					}
				}
			});
		}
	});
	
	// 确认退款
	$(document).on("click", ".refund-btn", function() {
		var id = $(this).parent().parent().attr("job-id");
		layer.confirm("确定要退款吗?", {
			icon: 3,
			title: '温馨提示'
		}, function(index) {
			if(id != null && id.length == 32) {
				// 查询退款信息
				$.ajax({
					url: "/refund/toRefund",
					type: "post",
					data : {
						"jobId": id
					},
					success: function(result) {
						if(result.status == 0) {
							layer.msg("退款成功");
							setTimeout("location.reload();", 2000);
						} else {
							layer.msg(result.msg, {
								icon : 5
							});
						}
					}
				});
			}
		});
	});

}();